#include <iostream>

using namespace std;

class node{
    public:
        int data;
        node* next;
        node(int val) : data(val), next(nullptr) {}
};

class LinkedList{
    private:
        node* head;
    public:
        LinkedList() : head(nullptr) {}

        void append(int val){   //表尾插入
            node* newnode = new node(val);
            if(head == nullptr){
                head = newnode;
            }else{
                node* cur = head;
                while(cur->next != nullptr){
                    cur = cur->next;
                }
                cur->next = newnode;
            }
        }

        void insert(int a, int val){    //在值为a的节点后面插入
            node* cur = head;
            node* newnode = new node(val);
            while(cur->next != nullptr){
                if(cur->data == a){
                    newnode->next = cur->next;
                    cur->next = newnode;
                    return;
                }
                cur = cur->next;
            }
            if(cur->next == nullptr && cur->data == a){//在最后面插入
                append(val);
                delete newnode;
            }
        }

        int findnext(int x){    //查找值为x的节点后面的那个节点的值
            node* cur = head;
            while(cur->next != nullptr){
                if(cur->data == x){
                    return cur->next->data;
                }
                cur = cur->next;
            }
            if(cur->next == nullptr && cur->data == x){
                return 0;
            }else{
                return -1;
            }
        }

        void print(){
            node* cur = head;
            cout << "----------------------LINKEDLIST----------------------\n";
            while(cur != nullptr){
                cout << cur->data << " " << "-> ";
                cur = cur->next; 
            }
            cout << "END\n";
        }

        void deletenext(int x){
            node* cur = head;
            while(cur->next != nullptr){
                if(cur->data == x){
                    node* tmp = cur->next;
                    cur->next = cur->next->next;
                    delete tmp;
                    tmp = nullptr;
                    return;
                }
                cur = cur->next;
            }
        }
};

int main(){
    LinkedList l;
    l.append(1);
    int q;
    cin >> q;
    for(int i = 1; i <= q; i++){
        int a, b, c;
        cin >> a;
        if(a == 1){
            cin >> b >> c;
            l.insert(b, c);
        }
        if(a == 2){
            cin >> b;
            cout << l.findnext(b) << "\n";
        }
        if(a == 3){
            cin >> b;
            l.deletenext(b);
        }
    }
    //l.print();
    return 0;
}